import i18n from '@/i18n'
import { AppLogger } from '@libs/lhy-common'
import { Module } from '@nestjs/common'
import { CoreConfigService } from '../core-config'
import { CoreCacheService } from './core-cache.service'
import { CoreCacheMemoryService } from './memory/core-cache-memory.service'
import { CoreCacheRedisService } from './redis/core-cache-redis.service'

/**
 * 缓存配置模块
 */
@Module({
  providers: [
    {
      provide: CoreCacheService,
      useFactory(configService: CoreConfigService) {
        switch (configService.config.cache.type) {
          case 'redis':
            AppLogger.info('CoreCacheModule', i18n.t('core-cache.core-cache.516240-0'), () =>
              JSON.stringify(configService.config.cache.redis)
            )
            return new CoreCacheRedisService(configService.config.cache.redis)
        }

        AppLogger.info('CoreCacheModule', i18n.t('core-cache.core-cache.516240-1'))
        return new CoreCacheMemoryService()
      },
      inject: [CoreConfigService],
    },
  ],
  exports: [CoreCacheService],
})
export class CoreCacheModule {}
